package org.rbac.action;

import org.rbac.serv.RoleServices;
import org.rbac.vo.ActionReturn;
import org.rbac.vo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 角色
 * 
 * <pre>
 * 项目名称：rbac
 * 类名称：RoleAction
 * 类描述：角色
 * 创建人：唐家林(tjl)
 * 创建时间：2014-9-25 上午9:08:17
 * 修改人：
 * 修改时间：
 * 修改备注：
 * @version
 * </pre>
 */
@Controller
@RequestMapping(value = "/role")
public class RoleAction {

	@Autowired
	private RoleServices roleServ;

	/**
	 * 修改
	 * @param r
	 *修改的对象数据
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/modify")
	@ResponseBody
	public String modifyRole(@ModelAttribute Role r) {
		ActionReturn ar = new ActionReturn();
		try {
			roleServ.moidifyRole(r);

			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 *            删除的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public String deleteRole(Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 0;
			roleServ.moidifyRoleStatus(ids, status);
			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 启用
	 * 
	 * @param ids
	 *            启用的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/enable")
	@ResponseBody
	public String enableRole(Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 1;
			roleServ.moidifyRoleStatus(ids, status);
			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 禁用
	 * 
	 * @param ids
	 *            禁用的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/disenable")
	@ResponseBody
	public String disenableRole(Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 2;
			roleServ.moidifyRoleStatus(ids, status);
			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 进入角色列表
	 * 
	 * @return 返回视图
	 */
	@RequestMapping(value = "/list")
	public ModelAndView role() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/rbac/role");
		return mav;
	}
	
	/**
	 * 角色的成员管理
	 * @return
	 */
	@RequestMapping(value = "/refUsers")
    public ModelAndView roleOfUsers() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/rbac/role_ref_user");
        return mav;
    }
	/**
	 * 添加角色成员
	 * @param userids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addRoleMember")
	public String addRoleMember(Integer roleId,String userids){
	    ActionReturn ar = null;
	    try {
	        roleServ.addRoleMember(roleId, userids);
	        ar=new ActionReturn(true,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            ar=new ActionReturn(false,"操作失败");
        }
	    return ar.toString();
	    
	}
	
	/**
     * 删除角色成员
     * @param userids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delRoleMember")
    public String delRoleMember(Integer ids[]){
        ActionReturn ar = null;
        try {
            roleServ.delRoleMember(ids);
            ar=new ActionReturn(true,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            ar=new ActionReturn(false,"操作失败");
        }
        return ar.toString();
        
    }
    
}
